1762G - Unequal Adjacent Elements - CodeForces Solution


constructive algorithms sortings *3100

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>
using namespace std;
int _,n,a[1<<20],s[1<<20],c[1<<20],u,A[1<<20];
void O(){
	cin>>n,u=0;
	for(int i=1;i<=n;i++)
		s[i]=c[i]=0;
	for(int i=1;i<=n;i++)
		cin>>a[i],s[++c[a[i]]]++,u=max(u,c[a[i]]);
	if(u>(n+1)/2){
		cout<<"NO\n";
		return;
	}
	cout<<"YES\n";
	for(int i=1,l=1,r=2;i<=n;i++){
		if(u==(n-l+2)/2)
			for(int j=1;j<=n;j++)
				if(c[j]==u){
					for(int k=i;k<=n;k++)
						if(a[k]==j)
							A[l]=k,l+=2;
						else
							A[r]=k,r+=2;
					for(int k=1;k<=n;k++)
						cout<<A[k]<<" \n"[k==n];
					return;
				}
		if(a[i]!=a[A[l-1]]){
			A[l]=i,l+=2;
			if(l>r)
				swap(l,r);
		}else
			A[r]=i,r+=2;
		s[c[a[i]]--]--,u-=!s[u];
	}
}
int main(){
	for(cin>>_;_--;)
		O();
}


Comments

Submit
0 Comments
More Questions

439A - Devu the Singer and Churu the Joker
1323A - Even Subset Sum Problem
1095A - Repeating Cipher
630F - Selection of Personnel
630K - Indivisibility
20B - Equation
600B - Queries about less or equal elements
1015A - Points in Segments
1593B - Make it Divisible by 25
680C - Bear and Prime 100
1300A - Non-zero
1475E - Advertising Agency
1345B - Card Constructions
1077B - Disturbed People
653A - Bear and Three Balls
794A - Bank Robbery
157A - Game Outcome
3B - Lorry
1392A - Omkar and Password
489A - SwapSort
932A - Palindromic Supersequence
433A - Kitahara Haruki's Gift
672A - Summer Camp
1277A - Happy Birthday Polycarp
577A - Multiplication Table
817C - Really Big Numbers
1355A - Sequence with Digits
977B - Two-gram
993A - Two Squares
1659D - Reverse Sort Sum